package com.wswy.wzcx.module;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearch.OnWeatherSearchListener;
import com.amap.api.services.weather.WeatherSearchQuery;
import com.blankj.utilcode.util.SPUtils;
import com.che.common.map.LocationModel;
import com.che.libcommon.api.ApiOptionalResultObserver;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.utils.RxBus;
import com.wswy.wzcx.AppContext;
import com.wswy.wzcx.api.Api;
import com.wswy.wzcx.model.weather.WeatherModel;
import com.wswy.wzcx.module.weather.AmapWeatherManager;
import com.wswy.wzcx.module.weather.WeatherUtils;
import io.reactivex.Observable;
import io.reactivex.observers.ResourceObserver;
import java.util.concurrent.TimeUnit;

public class WeatherManager {

  private static final String TAG = "WeatherManager";

  private static final String SP_KEY = "key_weather_city";

  private static class InstanceHolder{
    private static final WeatherManager holder = new WeatherManager();
  }

  public static WeatherManager getInstance(){

    return InstanceHolder.holder;
  }


  public void init(Context context) {

    WeatherUtils.INSTANCE.getWeatherBg("晴");
  }

  private WeatherManager() {

    reloadWeather();

  }


  public void start(){
    // TODO: 2018/1/18 每隔30分钟刷新
    Observable.interval(30,TimeUnit.MINUTES).subscribe(new ResourceObserver<Object>() {
      @Override
      public void onNext(Object o) {
        reloadWeather();
      }

      @Override
      public void onError(Throwable e) {

      }

      @Override
      public void onComplete() {

      }
    });
  }


  private void reloadWeather(){

    //上传保存的位置
    String city = LocManager.getInstance().getDisplayCityName();
    if (!TextUtils.isEmpty(city)){
      loadWeather(city);
    }
  }

  void loadWeather(String city){
    AmapWeatherManager.INSTANCE.loadWeather(city,true);

  }

}
